﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Exhibition.Common.CacheManager;
using Exhibition.Common.Helper;
using Exhibition.Dao.ViewModel;
using Exhibition.Service;

namespace Exhibition
{
    public partial class _AdminLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Response.Redirect("/Manage/Index.aspx");
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            var username = txtUserName.Text.Trim();
            var pwd = txtPwd.Text.Trim();
            //if (StringUtils.GetWebConfigValue("IsTest").ToLower() == "true")
            //{
            //    username = "admin";
            //    pwd = "admin";
            //}
            if (string.IsNullOrEmpty(username))
            {
                JsHelper.Alert(this, "对不起用户名不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(pwd))
            {
                JsHelper.Alert(this, "对不起密码不能为空!");
                return;
            }

           
            AdminUserModel mdl = CenterService.adminUserService.UserLogin(username, StringUtils.EncryptPassWord(pwd, StringUtils.PassFormat.MD5));
            if (mdl != null)
            {
                string UsCacheID = username.ToUpper() + "@@U";
                FormsAuthentication.SetAuthCookie(MembershipService.GenerateName(username), false);
                CommonCacheManager.AddCacheData<AdminUserModel>(UsCacheID, mdl, 180);
                //登陆的时候放入缓存
                SiteModel stmdl = CenterService.siteService.GetCurrentSite();
                CommonCacheManager.AddCacheData<SiteModel>("CurrentSite"+mdl.Id,stmdl,180);


                UserLogModel ulg = new UserLogModel();
                ulg.Content = "用户:" + mdl.UserName + " 登录系统，登录时间:" + DateTime.Now + ",登录IP:" + HttpContext.Current.Request.UserHostAddress;
                ulg.CreateTime = DateTime.Now;
                ulg.UserId = mdl.Id;

                CenterService.userLogService.Save(ulg);
                JsHelper.Redirect("Manage/Index.aspx","iframe");
            }

        }
    }
}